<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学科" prop="subject">
        <el-select v-model="queryParams.subject" placeholder="请选择学科" clearable>
          <el-option
            v-for="dict in dict.type.tea_subject_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="电话" prop="teacherPhone">
        <el-input
          v-model="queryParams.teacherPhone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帮助优先级" prop="priority">
        <el-input
          v-model="queryParams.priority"
          placeholder="请输入帮助优先级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="teacherType">
        <el-select v-model="queryParams.teacherType" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.tea_teacher_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="组长id" prop="groupLeaderId">
        <el-input
          v-model="queryParams.groupLeaderId"
          placeholder="请输入组长id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绩效类型" prop="performanceType">
        <el-select v-model="queryParams.performanceType" placeholder="请选择绩效类型" clearable>
          <el-option
            v-for="dict in dict.type.tea_teacher_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:teachers:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:teachers:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:teachers:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:teachers:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teachersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="teacherName" />
      <el-table-column label="学科" align="center" prop="subject">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tea_subject_type" :value="scope.row.subject"/>
        </template>
      </el-table-column>
      <el-table-column label="校区" align="center" prop="teacherSchool">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stu_class_address" :value="scope.row.teacherSchool"/>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="teacherPhone" />
      <el-table-column label="帮助优先级" align="center" prop="priority" />
      <el-table-column label="类型" align="center" prop="teacherType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tea_teacher_type" :value="scope.row.teacherType"/>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="teacherSex" >
      <template slot-scope="scope">
        <span v-if="scope.row.teacherSex==0" >男</span>
        <span v-else >女</span>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:teachers:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:teachers:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教师表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="钉钉Id" prop="teacherName">
          <el-input v-model="form.dingdingId" placeholder="请输入钉钉Id" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-select v-model="form.account" placeholder="请选关联账号">
            <el-option v-for="item in Users"
                       :label="item.userName"
                       :value="item.userName"
                       :key="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科" prop="subject">
          <el-select v-model="form.subject" placeholder="请选择学科">
            <el-option
              v-for="dict in dict.type.tea_subject_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="校区" prop="studentSchool">
          <el-select v-model="form.teacherSchool" placeholder="请选择校区">
            <el-option
              v-for="dict in dict.type.stu_class_address"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="teacherPhone">
          <el-input v-model="form.teacherPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="排序" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="类型" prop="teacherType">
          <el-select v-model="form.teacherType" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.tea_teacher_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.teacherSex">
            <el-radio class="radio" :label="0">男</el-radio>
            <el-radio class="radio" :label="1">女</el-radio>
            <el-radio class="radio" :label="2">未知</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="组长" prop="groupLeaderId">
          <el-select v-model="form.groupLeaderId" placeholder="请选择组长">
            <el-option v-for="item in groupTeachersList"
                       :label="item.teacherName"
                       :value="item.teacherId"
                       :key="item.teacherId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绩效类型" prop="performanceType">
          <el-select v-model="form.performanceType" placeholder="请选择绩效类型">
            <el-option
              v-for="dict in dict.type.tea_teacher_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeachers, getTeachers, delTeachers, addTeachers, updateTeachers } from "@/api/teacher/teachers";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect } from "@/api/system/user";
export default {
  name: "Teachers",
  dicts: ['tea_subject_type', 'tea_teacher_type','stu_class_address'],
  data() {
    return {
      //所有账号
      Users: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //组长表格数据
      groupTeachersList: [],
      // 教师表表格数据
      teachersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        dingdingId: null,
        subject: null,
        teacherPhone: null,
        priority: null,
        teacherType: null,
        teacherSex: null,
        groupLeaderId: null,
        account: null,
        performanceType: null,
        teacherSchool:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**查询所有组长老师*/
    getGroupTeachersList(){
      listTeachers(null).then(response => {
        this.groupTeachersList = response.rows;
      });
    },
    /**查询所有账号*/
    getUsers(){
      listUser(null).then(response => {
        this.Users = response.rows;
      });
    },
    /** 查询教师表列表 */
    getList() {
      this.loading = true;
      listTeachers(this.queryParams).then(response => {
        this.teachersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teacherId: null,
        dingdingId: null,
        teacherName: null,
        subject: null,
        teacherPhone: null,
        priority: null,
        teacherType: null,
        teacherSex: null,
        groupLeaderId: null,
        account: null,
        performanceType: null,
        teacherSchool:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teacherId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getGroupTeachersList();
      this.getUsers();
      this.open = true;
      this.title = "添加教师表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getGroupTeachersList();
      this.getUsers();
      const teacherId = row.teacherId || this.ids
      getTeachers(teacherId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教师表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.teacherId != null) {
            updateTeachers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeachers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teacherIds = row.teacherId || this.ids;
      this.$modal.confirm('是否确认删除教师表编号为"' + teacherIds + '"的数据项？').then(function() {
        return delTeachers(teacherIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/teachers/export', {
        ...this.queryParams
      }, `teachers_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
